﻿
using System;
using System.Collections;
using System.Collections.Generic;
using DotNetNeural.Data.Utils;
using System.Xml.Serialization;
namespace DotNetNeural.Data.Clusters
{
    public class Cluster : IEnumerable<float[]>
    {
        #region Properties

        public float[] Center { get; private set; }

        public float Dispersion { get; set; }

        public int ItemsCount
        {
            get
            {
                return items.Count;
            }
        }

        #endregion

        public Cluster(float[] center)
        {
            if (center == null)
                throw new NullReferenceException("Illegal null-reference cluster center");

            items = new List<float[]>();
            Center = center;
        }

        #region Public methods

        /// <summary>
        /// Adds given vector to cluster. Checks whether cluster doesn't contains already given vector
        /// </summary>
        public bool Add(float[] vec)
        {
            if (vec == null)
                return false;

            if (!items.Contains(vec))
            {
                items.Add(vec);
                return true;
            }

            return false;
        }

        /// <summary>
        /// Adds given vectors into cluster. Checks whether cluster doesn't contains already given vectors
        /// </summary>
        public bool Add(params float[][] items)
        {
            if (items == null)
                return false;

            foreach (var i in items)
            {
                if (!Add(i))
                    return false;
            }

            return true;
        }

        /// <summary>
        /// Gets value indicating whether cluster container given vector
        /// </summary>
        public bool Contains(float[] vec)
        {
            foreach (var f in items)
            {
                if (vec.Compare(f, ArrayExtensions.DefaultEqualityDelta))
                    return true;
            }

            return false;
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            return items.GetEnumerator();
        }

        public IEnumerator<float[]> GetEnumerator()
        {
            return items.GetEnumerator();
        }

        #endregion

        #region Private fields

        private List<float[]> items;

        #endregion
    }
}
